package com.gf.common.util;

import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics2D;
import java.awt.geom.AffineTransform;
import java.awt.image.BufferedImage;
import java.util.Random;

/**
 * 生成随机验证码
 * 
 * @author zhuguifeng
 * 
 */
public class CertCodeHelper {

	private static int width = 100;

	private static int height = 25;

	// 存储生成的验证码序列
	private static StringBuffer buffer = new StringBuffer();

	private static Random random = new Random();

	/**
	 * 生成带有验证码的图像
	 * @param fontSize 字体大小 
	 * @param length 字母个数
	 * @return
	 */
	public static BufferedImage getCertCodeImage(int fontSize,int length) {
		BufferedImage image = new BufferedImage(width, height,
				BufferedImage.TYPE_INT_RGB);
		Graphics2D g = image.createGraphics();

		// 设定字体样式
		Font font = new Font("宋体", Font.BOLD, fontSize);
		g.setFont(font);

		// 图片颜色
		g.setColor(getRanColor(210, 250));
		// 绘制背景
		g.fillRect(0, 0, width, height);
		g.setColor(getRanColor(190, 250));

		// 画线
		drawRanLines(g, 1);
		drawRanChara(g, length);
		g.dispose();
		
		return image;
	}

	/**
	 * 生成验证码<br>
	 * 使用前提：调用了getCertCodeImage()
	 * 
	 * @return
	 */
	public static String getCertCode() {
		return buffer.toString();
	}

	/**
	 * 生成随机的颜色
	 * 
	 * @param fc
	 *            前景色
	 * @param bc
	 *            背景色 <font color='red'> bc>fc </font>
	 * @return
	 */
	private static Color getRanColor(int fc, int bc) {

		if (fc > 255) {
			fc = 200;
		}
		if (bc > 255) {
			bc = 255;
		}

		int r = fc + random.nextInt(bc - fc);
		int g = fc + random.nextInt(bc - fc);
		int b = fc + random.nextInt(bc - fc);

		return new Color(r, g, b);
	}

	/**
	 * 往生成图形里画线，增加干扰度
	 * 
	 * @param g
	 * @param nums
	 */
	private static void drawRanLines(Graphics2D g, int nums) {
		g.setColor(getRanColor(170, 200));
		for (int i = 0; i < nums; i++) {
			int x1 = width;
			int y1 = random.nextInt(height);

			int x2 = random.nextInt(5);
			int y2 = random.nextInt(5);

			g.drawLine(x1, y1, x2, y2);
		}
	}

	/**
	 * 生成随机的数字<br>
	 * 暂时不包括中文
	 * 
	 * @param g
	 * @param length
	 */
	private static void drawRanChara(Graphics2D g, int length) {
		buffer = new StringBuffer();
		int itemp = 0;
		String stemp = "";

		for (int i = 0; i < length; i++) {
			switch (random.nextInt(3)) {
			// 小写字母
			case 0:
				itemp = random.nextInt(26) + 97;
				stemp = String.valueOf((char)itemp);
				break;
			// 大写字母
			case 1:
				itemp = random.nextInt(26) + 65;
				stemp = String.valueOf((char)itemp);
				break;

			// 数字
			default:
				itemp = random.nextInt(10) + 48;
				stemp = String.valueOf((char) itemp);
				break;
			}

			// 对生成的数字进行一定的倾斜
			Color color = new Color(20 + random.nextInt(20),
					20 + random.nextInt(20), 20 + random.nextInt(20));
			g.setColor(color);

			AffineTransform transform = new AffineTransform();
			transform.rotate(random.nextInt(30) * 3.14 / 180, 15 * i + 8, 7);
			// 缩放文字
			float sx = random.nextFloat() + 0.8f;
			if (sx > 1f) {
				sx = 1f;
			}
			transform.scale(sx, sx);
			g.setTransform(transform);
			g.drawString(stemp, 15 * i + 18, 14);
			buffer.append(stemp);
		}
		g.dispose();
	}
}
